//
//  FirstCollectionViewCell.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/23.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "FirstCollectionViewCell.h"
#import "ListenDataSongsModel.h"
#import "ManyListenListTableViewCell.h"
#import "PlayerViewController.h"
#import "Songer.h"
#import "MusicType.h"
#import "SongsAuditionListModel.h"
@interface FirstCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) CGFloat contenfOffSetY;

@end
@implementation FirstCollectionViewCell

- (void)dealloc
{
    [_tableView release];
    [_IDString release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.firstArray = [NSMutableArray array];
        [self createTableView];
    }
    return self;
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.contenfOffSetY = _tableView.contentOffset.y;
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 250)];
    headView.alpha = 0;
    self.tableView.tableHeaderView = headView;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = footView;
    
    [self.tableView registerClass:[ManyListenListTableViewCell class] forCellReuseIdentifier:@"firstCell"];
    
    [self.contentView addSubview:_tableView];
    [_tableView release];
}

- (void)couster
{
    NSString *string = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88243&longtitude=121.54", _IDString];
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            ListenDataSongsModel *listen = [[ListenDataSongsModel alloc] initWithDictionary:dic];
            
            if (listen.auditionList.count == 0) {
                
            }else{
                [self.firstArray addObject:listen];
            }
            [listen release];
        }
        [self.tableView reloadData];
    }];
}

- (void)setIDString:(NSString *)IDString
{
    if (_IDString != IDString) {
        [_IDString release];
        _IDString = [IDString retain];
        [self couster];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _firstArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ManyListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.dataSongsModel = [self.firstArray objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.height / 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    NSMutableArray *arr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < self.firstArray.count; i++)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:[_firstArray[i] albumName] forKey:@"album"];// 专辑
        [dic setValue:[_firstArray[i] name] forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *song in [self.firstArray[i] auditionList]) {
            MusicType *music = [[MusicType alloc] init];
            music.url = song.urlString;
            music.typeDescription = @"压缩品质";
            [array addObject:music];
            [music release];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:[_firstArray[i] singerName] forKey:@"songerName"];// 歌手名
        [dic setValue:[_firstArray[i] albumId] forKey:@"id"]; // id
        [arr addObject:dic];
    }
    player.musicArr = arr;
    [player MusicStart:indexPath.row];
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y < _contenfOffSetY) {
//        [self.delegate FirstTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:NO];
//    }else {
//        [self.delegate FirstTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:YES];
//    }
//    _contenfOffSetY = scrollView.contentOffset.y;
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
